package com.rran.study.chat.ops.config;

import com.rran.study.chat.common.constant.CommonConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //添加header参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        // 请求头-beanId
        Parameter headerBeanId = parameterBuilder.name(CommonConstant.HEADER_BEANID_KEY).description(CommonConstant.HEADER_BEANID_VALUE)
                .modelRef(new ModelRef(CommonConstant.SWAGGER_PARAM_DATA_TYPE_STR)).parameterType(CommonConstant.SWAGGER_PARAM_PARAM_TYPE_HEADER)
                .required(true).build();

        pars.addAll(Arrays.asList(headerBeanId));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rran.study.chat.server.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("实时聊天系统").description("用于前后端对接").version("1.01")
                .build();
    }
}
